<?php
namespace App;
defined('IN_IA') or exit('Access Denied');
use \Pub\cache\Cache;
class Plugins extends Action
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 创建插件列表
     */
    public function getPlugins()
    {
        $_list = Cache::get("kuyuan_fm_plugins");
        $this->_result = array("status"=>"success","cache"=>1,"data"=>$_list);
        if(!$_list || KUYUAN_DEBUG){
            $_list = array(
                array(
                    "name"=>"地址管理",
                    "value"=>"address",
                    "brief"=>"前端用户收货地址统一管理",
                    "charge"=>0,
                    "table"=>"kuyuan_fm_plugin_address_address",
                    "api"=>wurl('site/entry/api',array('m'=>'kuyuan_fm_plugin_address')),
                    "install_url"=>"https://s.w7.cc/module-26249.html"
                ),
                array(
                    "name"=>"每日自动送积分",
                    "value"=>"auto",
                    "brief"=>"关注公众号每天自动送积分，取消关注每日扣除积分，开启本插件后每日手动签到功能自动关闭",
                    "charge"=>1,
                    "table"=>"kuyuan_fm_plugin_auto_setting",
                    "api"=>wurl('site/entry/api',array('m'=>'kuyuan_fm_plugin_auto')),
                    "install_url"=>"https://s.w7.cc/module-25836.html"
                )
            );
            foreach($_list as $_k=>$_v){
                $_list[$_k]['install'] = pdo_tableexists($_v['table']) ? 1 : 0;
            }
            $_install = $_uninstall = array();
            foreach($_list as $_v){
                if($_v['install']){
                    $_install[] = $_v;
                }else{
                    $_uninstall[] = $_v;
                }
            }
            $_list = array_merge($_install,$_uninstall);
            $this->_result["cache"] = 0;
            $this->_result["data"] = $_list;
            Cache::set("kuyuan_fm_plugins",$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 更新插件缓存
     */
    public function updatePlugin()
    {
        Cache::remove("kuyuan_fm_plugins");
        $this->_result = array("status"=>"success");
        $this->result($this->_result);
    }

    /**
     * 根据插件标记返回插件API请求地址
     * @param string name 插件标识
     */
    public function getApi()
    {
        $this->_result = array("status"=>"success","data"=>murl('entry//api',array('m'=>'kuyuan_fm_plugin_'.$this->_gpc["name"]),true,true));
        $this->result($this->_result);
    }
}